// src/api/member.js
// 党员信息管理系统 - 所有接口封装（全部 POST）

import { postOk } from './http.js';
import { USE_MOCK_DATA } from './config.js';
import { mockAPI } from './mock.js';

const API = {
  // 1. 党员基本操作
  list: '/member/list',
  detail: '/member/detail', // 获取党员详情接口
  create: '/member/create',
  delete: '/member/delete',

  // 2. 基本信息修改
  updateBasicInfo: '/member/update-basic-info',

  // 3. 档案信息修改
  updateApplicationDocs: '/member/update-application-docs',
  updateActivistAssessment: '/member/update-activist-assessment',
  updateApplicationVolunteer: '/member/update-application-volunteer',
  updateProbationaryDocs: '/member/update-probationary-docs',

  // 4.1 党支部历史
  pbHistoryCreate: '/member/party-branch-history/create',
  pbHistoryUpdate: '/member/party-branch-history/update',
  pbHistoryDelete: '/member/party-branch-history/delete',

  // 4.2 志愿活动
  vaCreate: '/member/volunteer-activity/create',
  vaUpdate: '/member/volunteer-activity/update',
  vaDelete: '/member/volunteer-activity/delete',

  // 4.3 获奖记录
  awardCreate: '/member/award/create',
  awardUpdate: '/member/award/update',
  awardDelete: '/member/award/delete',

  // 4.4 培养人
  mentorCreate: '/member/mentor/create',
  mentorUpdate: '/member/mentor/update',
  mentorDelete: '/member/mentor/delete',

  // 4.5 考察记录
  arCreate: '/member/assessment-record/create',
  arUpdate: '/member/assessment-record/update',
  arDelete: '/member/assessment-record/delete',

  // 4.6 思想汇报
  atrCreate: '/member/activist-thought-report/create',
  atrUpdate: '/member/activist-thought-report/update',
  atrDelete: '/member/activist-thought-report/delete',
  ptrCreate: '/member/probationary-thought-report/create',
  ptrUpdate: '/member/probationary-thought-report/update',
  ptrDelete: '/member/probationary-thought-report/delete',

  // 4.7 外调记录
  eiCreate: '/member/external-investigation/create',
  eiUpdate: '/member/external-investigation/update',
  eiDelete: '/member/external-investigation/delete',

  // 4.8 预备党员考察意见（半年/一年）
  probationaryAssessmentOpinionUpdate: '/member/probationary-assessment-opinion/update',

  // 5. 批量导入
  batchImport: '/member/batch-import',

  // 6.2 基础筛选选项
  filterOptions: '/member/filter-options',
};

/* -----------------------------------------
 * 1. 党员基本操作
 * --------------------------------------- */

/** 1.1 获取党员列表（分页） */
export function fetchMemberList(payload) {
  // payload 结构参考文档
  if (USE_MOCK_DATA) {
    return mockAPI.member.list(payload);
  }
  return postOk(API.list, payload);
}

/** 1.1.1 获取所有党员（不分页） */
export function getAllMembers() {
  // 获取所有党员列表，不分页
  if (USE_MOCK_DATA) {
    return mockAPI.member.list({ page: 1, pageSize: 1000 });
  }
  return postOk(API.list, { page: 1, pageSize: 1000 });
}

/** 1.1.2 获取党员详情 */
export function fetchMemberDetail(memberId) {
  // 获取单个党员的详细信息
  if (USE_MOCK_DATA) {
    return mockAPI.member.detail({ memberId });
  }
  return postOk(API.detail, { memberId });
}

/** 1.2 新增党员 */
export function createMember(payload) {
  // { memberName, studentId, idCardNumber, phoneNumber }
  if (USE_MOCK_DATA) {
    return mockAPI.member.create(payload);
  }
  return postOk(API.create, payload);
}

/** 1.3 删除党员 */
export function deleteMember(memberId) {
  if (USE_MOCK_DATA) {
    return mockAPI.member.delete({ memberId });
  }
  return postOk(API.delete, { memberId });
}

/* -----------------------------------------
 * 2. 基本信息修改
 * --------------------------------------- */

/** 2.1 修改党员基本信息 */
export function updateMemberBasicInfo(memberId, updateData) {
  return postOk(API.updateBasicInfo, { memberId, updateData });
}

/* -----------------------------------------
 * 3. 档案信息修改
 * --------------------------------------- */

/** 3.1 修改入党相关档案（applicationForm、heartToHeartTalk、activistRecommendation、massOpinion、autobiography、comprehensiveReview、developmentTargetPrereview） */
export function updateApplicationDocs(memberId, documentType, updateData) {
  console.log('API - updateApplicationDocs called with:', { memberId, documentType, updateData });
  return postOk(API.updateApplicationDocs, { memberId, documentType, updateData });
}

/** 3.2 修改积极分子考察表 */
export function updateActivistAssessment(memberId, updateData) {
  return postOk(API.updateActivistAssessment, { memberId, updateData });
}

/** 3.3 修改入党志愿书 */
export function updateApplicationVolunteer(memberId, updateData) {
  return postOk(API.updateApplicationVolunteer, { memberId, updateData });
}

/** 3.4 修改预备党员相关档案（probationaryAcceptanceVoting、developmentPublicity、conversionApplication、conversionPublicity、conversionVotingSummary） */
export function updateProbationaryDocs(memberId, documentType, updateData) {
  return postOk(API.updateProbationaryDocs, { memberId, documentType, updateData });
}

/* -----------------------------------------
 * 4. 嵌套列表管理
 * --------------------------------------- */

/** 4.1.1 新增党支部历史记录 */
export function createPartyBranchHistory(memberId, historyData) {
  return postOk(API.pbHistoryCreate, { memberId, historyData });
}

/** 4.1.2 修改党支部历史记录 */
export function updatePartyBranchHistory(memberId, historyId, updateData) {
  return postOk(API.pbHistoryUpdate, { memberId, historyId, updateData });
}

/** 4.1.3 删除党支部历史记录 */
export function deletePartyBranchHistory(memberId, historyId) {
  return postOk(API.pbHistoryDelete, { memberId, historyId });
}

/** 4.2.1 新增志愿活动记录 */
export function createVolunteerActivity(memberId, activityData) {
  return postOk(API.vaCreate, { memberId, activityData });
}

/** 4.2.2 修改志愿活动记录 */
export function updateVolunteerActivity(memberId, activityId, updateData) {
  return postOk(API.vaUpdate, { memberId, activityId, updateData });
}

/** 4.2.3 删除志愿活动记录 */
export function deleteVolunteerActivity(memberId, activityId) {
  return postOk(API.vaDelete, { memberId, activityId });
}

/** 4.3.1 新增获奖记录 */
export function createAward(memberId, awardData) {
  return postOk(API.awardCreate, { memberId, awardData });
}

/** 4.3.2 修改获奖记录 */
export function updateAward(memberId, awardId, updateData) {
  return postOk(API.awardUpdate, { memberId, awardId, updateData });
}

/** 4.3.3 删除获奖记录 */
export function deleteAward(memberId, awardId) {
  return postOk(API.awardDelete, { memberId, awardId });
}

/** 4.4.1 新增培养人 */
export function createMentor(memberId, mentorData) {
  return postOk(API.mentorCreate, { memberId, mentorData });
}

/** 4.4.2 修改培养人信息 */
export function updateMentor(memberId, mentorId, updateData) {
  return postOk(API.mentorUpdate, { memberId, mentorId, updateData });
}

/** 4.4.3 删除培养人 */
export function deleteMentor(memberId, mentorId) {
  return postOk(API.mentorDelete, { memberId, mentorId });
}

/** 4.5.1 新增考察记录 */
export function createAssessmentRecord(memberId, recordData) {
  return postOk(API.arCreate, { memberId, recordData });
}

/** 4.5.2 修改考察记录 */
export function updateAssessmentRecord(memberId, recordId, updateData) {
  return postOk(API.arUpdate, { memberId, recordId, updateData });
}

/** 4.5.3 删除考察记录 */
export function deleteAssessmentRecord(memberId, recordId) {
  return postOk(API.arDelete, { memberId, recordId });
}

/** 4.6.1 新增积极分子思想汇报 */
export function createActivistThoughtReport(memberId, reportData) {
  return postOk(API.atrCreate, { memberId, reportData });
}

/** 4.6.2 修改积极分子思想汇报 */
export function updateActivistThoughtReport(memberId, reportId, updateData) {
  return postOk(API.atrUpdate, { memberId, reportId, updateData });
}

/** 4.6.3 删除积极分子思想汇报 */
export function deleteActivistThoughtReport(memberId, reportId) {
  return postOk(API.atrDelete, { memberId, reportId });
}

/** 4.6.4 新增预备期思想汇报 */
export function createProbationaryThoughtReport(memberId, reportData) {
  return postOk(API.ptrCreate, { memberId, reportData });
}

/** 4.6.5 修改预备期思想汇报 */
export function updateProbationaryThoughtReport(memberId, reportId, updateData) {
  return postOk(API.ptrUpdate, { memberId, reportId, updateData });
}

/** 4.6.6 删除预备期思想汇报 */
export function deleteProbationaryThoughtReport(memberId, reportId) {
  return postOk(API.ptrDelete, { memberId, reportId });
}

/** 4.7.1 新增外调记录 */
export function createExternalInvestigation(memberId, investigationData) {
  return postOk(API.eiCreate, { memberId, investigationData });
}

/** 4.7.2 修改外调记录 */
export function updateExternalInvestigation(memberId, investigationId, updateData) {
  return postOk(API.eiUpdate, { memberId, investigationId, updateData });
}

/** 4.7.3 删除外调记录 */
export function deleteExternalInvestigation(memberId, investigationId) {
  return postOk(API.eiDelete, { memberId, investigationId });
}

/** 4.8 修改预备党员考察意见（半年/一年） */
export function updateProbationaryAssessmentOpinion(memberId, assessmentPeriod, opinionData) {
  return postOk(API.probationaryAssessmentOpinionUpdate, {
    memberId,
    assessmentPeriod, // 'half_year' | 'full_year'
    opinionData,
  });
}

/* -----------------------------------------
 * 5. 批量操作
 * --------------------------------------- */

/** 5.1 批量导入党员信息 */
export function batchImport(fieldType, importData) {
  return postOk(API.batchImport, { fieldType, importData });
}

/* -----------------------------------------
 * 6. 辅助接口
 * --------------------------------------- */

/** 6.2 获取基础筛选选项（不含统计） */
export function fetchFilterOptions() {
  // 文档声明所有接口都 POST，这里按 {} POST
  if (USE_MOCK_DATA) {
    return mockAPI.member.filterOptions();
  }
  return postOk(API.filterOptions, {});
}
